<?php
if($_GET['mode']=="edit"){
   if($_POST['submit_module']!=true) echo '';
   else {
      $select_u = mysql_query('SELECT teacher_to FROM users WHERE username = "'.$_SESSION['username'].'"');
      $row_u = mysql_fetch_assoc($select_u);
	  
      $module_name = addslashes(trim(htmlspecialchars($_POST['module_name'])));
	  $module_descr = addslashes(trim(htmlspecialchars($_POST['module_descr'])));
	  
	  if(empty($module_name) || empty($module_descr)){
	     echo '<p class="error">Моля, попълнете ВСИЧКИ полета!</p>';
	  } else {
	     $sel_m = mysql_query('SELECT name FROM modules WHERE name = "'.$module_name.'" AND to_tutorial = "'.$row_u['teacher_to'].'" AND id != '.$_GET['id'].'');
		 if(mysql_num_rows($sel_m) != 0){
		    echo '<p class="error">Името на раздела е заето!</p>';
		 } else {
		    if(strlen($module_name)<3 || strlen($module_descr)<3){
			   echo '<p class="error">Кратко име и/или описание</p>';
			} else {
			   $update='UPDATE modules SET name="' . $module_name. '", descr="'. $module_descr . '" WHERE id='.$_GET['id'].'';
		       $res_update=mysql_query($update);
			   echo '<p class="success">Успешно обновяване!</p>';
			   echo '<meta http-equiv="refresh" content="1;url=editmodule.php">';
			} 
		 }
	  }
   }
}
?>